package ua.kharkiv.knure.karimov.Task2;

public class Matrix {
	private double[][] data;

	public Matrix() {
		data = new double[4][4];
	}
	public Matrix(int n, int m) {
		data = new double[n][m];
	}

	public Matrix(double[][] data) {
		this.data = data;
	}

	public static Matrix sum(Matrix a, Matrix b) {
		if (a.data.length != b.data.length
				|| a.data[0].length != b.data[0].length)
			throw new IndexOutOfBoundsException();

		Matrix c = new Matrix();
		for (int i = 0; i < a.data.length; i++)
			for (int j = 0; j < a.data[0].length; j++)
				c.data[i][j] = a.data[i][j] + b.data[i][j];
		return c;
	}

	public static Matrix mul(Matrix a, Matrix b) {
		if (a.data.length != b.data[0].length
				|| a.data[0].length != b.data.length)
			throw new IndexOutOfBoundsException();
		Matrix c = new Matrix();
		for (int i = 0; i < a.data.length; i++)
			for (int j = 0; j < a.data[0].length; j++)
				for (int k = 0; k < a.data[0].length; k++)
					c.data[i][j] += a.data[i][k] * b.data[k][j];
		return c;
	}

	public static Matrix mul(Matrix a, double b) {
		Matrix c = new Matrix();
		for (int i = 0; i < a.data.length; i++)
			for (int j = 0; j < a.data[0].length; j++)
				c.data[i][j] = a.data[i][j] * b;
		return c;
	}

	public static Matrix mul(double b, Matrix a) {
		return mul(a, b);
	}

	public double get(int i, int j) {
		return data[i][j];
	}

	public void set(int i, int j, int value) {
		data[i][j] = value;
	}

	public int lengthCol() {
		return data.length;
	}

	public int LengthRow() {
		return data[0].length;
	}

	@Override
	public String toString() {
		StringBuilder s = new StringBuilder();
		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[i].length; j++){
				s.append(data[i][j]);
				s.append(' ');
			}
			s.append('\n');
		}
		return s.toString();
	}

	public static Matrix E() {
		Matrix m = new Matrix();
		for (int i = 0; i < 4; i++)
			m.data[i][i] = 1;
		return m;
	}

	public static Matrix trans(Matrix a) {
		Matrix b = new Matrix( a.data.length, a.data[0].length);
		for (int i = 0; i < a.data.length; i++)
			for (int j = 0; j < a.data[i].length; j++) {
				b.data[i][j] = a.data[j][i];
			}
		return b;
	}

	public void print() {
		System.out.println(this);
	}
}
